/**
 * 车辆信息管理初始化
 */
var CarInfo = {
    id: "CarInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
CarInfo.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '编号', field: 'id', visible: true, align: 'center', valign: 'middle'},
            {title: '车牌号', field: 'plateNumber', visible: true, align: 'center', valign: 'middle'},
            {title: '车类型', field: 'type', visible: true, align: 'center', valign: 'middle',formatter:function(value,row){
                    if (value==0){
                        return'公车'
                    } else if (value == 1){
                        return'私家车'
                    }  else {
                        return'状态异常'
                    }
                }},
            {title: '车辆状态', field: 'status', visible: true, align: 'center', valign: 'middle',formatter:function(value,row){

                            if (value==0){
                                return'可使用'
                            } else if (value == 1){
                                return'在途'
                            } else if (value == 2){
                                return'常用'
                            }else if (value == 3){
                                return'已预约'
                            } else {
                                return'状态异常'
                            }
                }},
            {title: '车辆描述', field: 'description', visible: true, align: 'center', valign: 'middle'},
            {title: '', field: 'info', visible: true, align: 'center', valign: 'middle',formatter: function (value,row){

                        return '<button type="button" class="btn btn-primary button-margin" onclick="CarInfo.openCarInfoDetail1(' + row.id + ')" id=""><i class="fa fa-info"></i>&nbsp;详情</button>';

                }
            }
    ];
};

/**
 * 检查是否选中
 */
CarInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CarInfo.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加车辆信息
 */
CarInfo.openAddCarInfo = function () {
    var index = layer.open({
        type: 2,
        title: '添加车辆信息',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/carInfo/carInfo_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看车辆信息详情
 */
CarInfo.openCarInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '车辆信息详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/carInfo/carInfo_update/' + CarInfo.seItem.id
        });
        this.layerIndex = index;
    }
};


/**
* 查看车辆信息详情
*/
CarInfo.openCarInfoDetail1 = function (rowId) {
    // console.log(rowId);
        var index = layer.open({
            type: 2,
            title: '车辆信息详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/carInfo/carInfo_info/' + rowId
        });
        this.layerIndex = index;
};
/**
 * 删除车辆信息
 */
CarInfo.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/carInfo/delete", function (data) {
            Feng.success("删除成功!");
            CarInfo.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("carInfoId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询车辆信息列表
 */
CarInfo.search = function () {
    var queryData = {};
        queryData['plateNumber'] = $("#plate").val();
        queryData['type'] = $("#type").val();
        queryData['status'] = $("#status").val();

    CarInfo.table.refresh({query: queryData});
};

CarInfo.download = function() {
    var queryData = {};
    queryData['plateNumber'] = $("#plate").val();
    queryData['type'] = $("#type").val();
    queryData['status'] = $("#status").val();
    window.location.href="/carInfo/download?plateNumber=" +queryData['plateNumber']+"&type="+queryData['type']+"&status="+queryData['status'];
}

$(function () {
    var defaultColunms = CarInfo.initColumn();
    var table = new BSTable(CarInfo.id, "/carInfo/list", defaultColunms);
    table.setPaginationType("client");
    CarInfo.table = table.init();
});

